import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

import com.cup.newSecurity.serviceImpl.AttributeServiceImpl;
import com.cup.newSecurity.serviceImpl.BaseUserInfoOM;
import com.cup.newSecurity.serviceImpl.LoginServiceImpl;
import com.cup.newSecurity.serviceImpl.UserServiceImpl;
import com.cup.newSecurity.util.LdapServer;

public class LdapMain {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		CommandLineParser parser = new PosixParser();
		Options options = new Options();
		options.addOption("l", false, "login action");
		options.addOption("u", true, "the user to login");
		options.addOption("o", true, "ou");
		options.addOption("c", false, "change password action");
		options.addOption("p", true, "password");
		options.addOption("n", true, "new password");
		options.addOption("i", true, "sys id");
		options.addOption("s", true, "attribute priting action, the value is the name of attribute");
		options.addOption("a", true, "add attribute action, the name is the name of attribute");
		options.addOption("v", true, "the value of attribute");

		CommandLine line = parser.parse(options, args);

		if (line.hasOption('l')) {
			String user = line.getOptionValue('u');
			String ou = line.getOptionValue('o');
			String password = line.getOptionValue('p');
			String sysId = line.getOptionValue('i');

			LoginServiceImpl loginSrv = new LoginServiceImpl();
			int expire = loginSrv.login(BaseUserInfoOM.create(user, ou),
					password, sysId, LdapServer.getInstance().getServerName());
			System.out.println("Login is OK. The expire is " + expire);
		} else if (line.hasOption('c')) {
			String user = line.getOptionValue('u');
			String ou = line.getOptionValue('o');
			String password = line.getOptionValue('p');
			String newPassword = line.getOptionValue('n');

			UserServiceImpl userSrv = new UserServiceImpl();
			userSrv.modifyUserPassword(BaseUserInfoOM.create(user, ou),
					password, newPassword);
			System.out.println("Change password is OK.");
		} else if (line.hasOption('s')) {
			String user = line.getOptionValue('u');
			String ou = line.getOptionValue('o');
			String attribute = line.getOptionValue('s');

			AttributeServiceImpl attrSrv = new AttributeServiceImpl();
			List<String> attributes = attrSrv.search(BaseUserInfoOM.create(user, ou), attribute);
			System.out.println(attribute + ":");
			for (String string : attributes) {
				System.out.println(string);
			}
		} else if (line.hasOption('a')) {
			String user = line.getOptionValue('u');
			String ou = line.getOptionValue('o');
			String attribute = line.getOptionValue('a');

			String value = line.getOptionValue('v');
			AttributeServiceImpl attrSrv = new AttributeServiceImpl();
			attrSrv.add(BaseUserInfoOM.create(user, ou), attribute, value);
			System.out.println("Attribute is added OK.");
		} else {
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp("LdapMain", "Options:", options, "Eg.\n case 1:\n  User login: LdapMain -l -u user1 -o 0800010001 -i 000 -p 111111\ncase 2:\n  Change password: LdapMain -c -u user1 -o 0800010001 -p 111111 -n 123456\ncase 3:\n  Search attribute: LdapMain -s privilege -u user1 -o 0800010001\ncase 4:\n  add attribute: LdapMain -a privilege -v \"sysid=000,rolegroup=000401,ou=0800010000,status=1,modifiersname=90000001,nslicensestarttime=20090623\" -u user1 -o 0800010001");
		}
	}

}
